package com.karaku.karaku_collector.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.karaku.karaku_collector.App;
import com.karaku.karaku_collector.LocalSetting;
import com.karaku.karaku_collector.R;
import com.karaku.karaku_collector.common.binder.BindView;
import com.karaku.karaku_collector.common.binder.UIBinder;
import com.karaku.karaku_collector.util.CameraUtil;
import com.karaku.karaku_collector.util.Size;
import com.karaku.karaku_collector.util.actions.Action1;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guopi on 2017/11/3 0003.
 */

@BindView(R.layout.view_camera_size_selector)
public class CameraSizeSelectorView extends FrameLayout {

    @BindView(R.id.lv_sizes)
    private ListView lvSizes;

    private List<Size> sizes;
    private Action1<Size> callback;

    public CameraSizeSelectorView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        UIBinder.bind(this, this, true);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });
    }

    public void show(final Action1<Size> callback) {
        this.callback = callback;
        setVisibility(VISIBLE);

        sizes = CameraUtil.getCameraSizes();
        lvSizes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (callback != null) {
                    callback.run(sizes.get(position));
                    hide();
                }
            }
        });
        lvSizes.setAdapter(new Adapter());
    }

    public void hide() {
        setVisibility(GONE);
        callback = null;
    }


    class Adapter extends BaseAdapter {
        @Override
        public int getCount() {
            return sizes.size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return sizes.get(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(App.getInstance()).inflate(R.layout.item_size, null);
            }
            TextView tv = (TextView) convertView.findViewById(R.id.tv_size);
            Size s = sizes.get(position);
            tv.setText(String.format("%d * %d        1/%.2f", s.height, s.width, s.width * 1.f / s.height));

            Size ls = LocalSetting.getInstance().getPreviewSize();
            if (ls.width == s.width && ls.height == s.height) {
                tv.setBackgroundColor(0XFFEEEEEE);
            } else {
                tv.setBackgroundColor(0X00EEEEEE);
            }
            return convertView;
        }
    }

}
